package com.wangzhixuan.controller;

import com.wangzhixuan.db.bean.Customer;
import com.wangzhixuan.db.bean.Department;
import com.wangzhixuan.db.bean.Doctor;
import com.wangzhixuan.service.DepartmentService;
import com.wangzhixuan.service.DoctorService;
import com.wangzhixuan.util.HttpUtils;
import com.wangzhixuan.vo.JsonVo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpPayService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class DoctorController  extends BaseController{
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long dept=0;
        try {
            request.setCharacterEncoding("utf-8");
            if(request.getParameter("dept")!=null){
                String deptStr = request.getParameter("dept");
                dept=Long.parseLong(deptStr);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        modelMap.put("deptSelected", dept);
        String type=request.getParameter("type");
        if(type==null){
            type="1";
        }
        modelMap.put("type", type);
        List<Department> depts=departmentService.getBaseDepartment();
        modelMap.put("depts", depts);
        return "list";
    }

    @ResponseBody
    @RequestMapping(value = "/getChildDept.json", method = RequestMethod.POST)
    public JsonVo<List<Department>> getChildDept(HttpServletRequest request, HttpServletResponse response,long parentId) {
        JsonVo<List<Department>> vo=new JsonVo<List<Department>>();
        List<Department> doctors=departmentService.getDepartmentsByParentId(parentId);
        vo.setResult(true);
        vo.setT(doctors);
        return vo;
    }

    @ResponseBody
    @RequestMapping(value = "/search.json", method = RequestMethod.POST)
    public JsonVo<List<Doctor>> search(HttpServletRequest request, HttpServletResponse response,String search) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JsonVo<List<Doctor>> vo=new JsonVo<List<Doctor>>();
        List<Doctor> doctors=doctorService.getDoctorBySearch(search);
        vo.setResult(true);
        vo.setT(doctors);
        return vo;
    }
    
    @ResponseBody
    @RequestMapping(value = "/dept.json", method = RequestMethod.POST)
    public JsonVo<List<Doctor>> dept(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, String deptName) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(deptName);
        JsonVo<List<Doctor>> vo=new JsonVo<List<Doctor>>();
        List<Doctor> doctors=doctorService.getDoctorByDepartment(deptName);
        vo.setResult(true);
        vo.setT(doctors);
        return vo;
    }

    @ResponseBody
    @RequestMapping(value = "/listByDept.json", method = RequestMethod.POST)
    public JsonVo<List<Doctor>> listByDept(HttpServletRequest request, HttpServletResponse response,
                                           ModelMap modelMap, long parentId,long childId) {
        JsonVo<List<Doctor>> vo=new JsonVo<List<Doctor>>();
        List<Doctor> doctors=new ArrayList<>();
        if(childId==0){
            if(parentId==0){
                doctors=doctorService.getAllDoctor();
            }else{
                List<Long> ids=new ArrayList<>();
                List<Department> depts=departmentService.getDepartmentsByParentId(parentId);
                for (int i = 0; i <depts.size() ; i++) {
                    Department dept=depts.get(i);
                    ids.add(dept.getId());
                }
                doctors=doctorService.getDoctorByDepartmentIds(ids);
            }
        }else{
            doctors=doctorService.getDoctorByDepartmentId(childId);
        }
        vo.setResult(true);
        vo.setT(doctors);
        return vo;
    }

    @RequestMapping(value = "/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, long doctorId) {
        Customer currentCustomer = getCurrentCustomer(request);
        long customerId=1;
        if(currentCustomer!=null){
            customerId=currentCustomer.getId();
        }
        Doctor doctor=doctorService.getDoctorDetail(doctorId);
        int notice=doctorService.getAttention(customerId, doctorId);

        modelMap.put("doctor", doctor);
        modelMap.put("notice", notice);
      
        return "detail";
    }
    
    @ResponseBody
    @RequestMapping(value = "/addNotice.json", method = RequestMethod.GET)
    public JsonVo<String> addNotice(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, long doctorId) {
        Customer currentCustomer = getCurrentCustomer(request);
        JsonVo<String> vo=new JsonVo<String>();
        long customerId=1;
        if(currentCustomer!=null){
            customerId=currentCustomer.getId();
        }
        doctorService.addAttention(customerId,doctorId);
        vo.setResult(true);
        vo.setT("");
        return vo;
    }
    
    @ResponseBody
    @RequestMapping(value = "/deleteNotice.json", method = RequestMethod.GET)
    public JsonVo<String> deleteNotice(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, long doctorId) {
        Customer currentCustomer = getCurrentCustomer(request);
        JsonVo<String> vo=new JsonVo<String>();
        long customerId=1;
        if(currentCustomer!=null){
            customerId=currentCustomer.getId();
        }
        doctorService.deleteAttention(customerId,doctorId);
        vo.setResult(true);
        vo.setT("");
        return vo;
    }
    @ResponseBody
    @RequestMapping(value = "/icon.img",method = RequestMethod.GET,produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] seeicon(@RequestParam("doctorId")Long doctorId,HttpServletRequest request,HttpServletResponse response){
        Doctor doctor=doctorService.getDoctorById(doctorId);
        byte[] icon = null;
        try {
            if (doctor != null) {
                icon = doctor.getIcon();
                if (icon == null) {
                    System.out.println(HttpUtils.getBasePath(request));
                    String defaultImg = "/static/images/default-icon.jpg";
                    InputStream in = this.getClass().getClassLoader().getResourceAsStream(defaultImg);
                    icon = IOUtils.toByteArray(in);
                }
                response.setContentType("image/png");
                OutputStream stream;
                stream = response.getOutputStream();
                stream.write(icon);
                stream.flush();
                stream.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return icon;
    }
}
